package com.qau.zek.controller;

import com.qau.zek.pojo.goods;
import com.qau.zek.utils.JDBCUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
@WebServlet("/goods")
public class GoodServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // JDBC
        System.out.println("GoodServlet.service()");
        //解决post请求中文乱码问题
        request.setCharacterEncoding("utf-8");
        String method = request.getParameter("method");
        switch (method){
            case "add_goods":
                add_goods(request,response);
                break;
            case "select_goods":
                select_goods(request,response);
                break;
            case "deleteById":
                deleteById(request,response);
                break;
            case "selectById":
                selectById(request,response);
                break;
            case "update":
                update(request,response);
                break;
            default:
                break;
        }
    }

    private void update(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("UserServlet.update()");
        String good_id = request.getParameter("good_id");
        String good_name = request.getParameter("good_name");
        String good_spec = request.getParameter("good_spec");
        String good_price = request.getParameter("good_price");
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "update goods set good_name=?,good_spec=?,good_price=? where good_id=?";
            statement = connection.prepareStatement(sql);
            statement.setString(4, good_id);
            statement.setString(1, good_name);
            statement.setString(2, good_spec);
            statement.setString(3,good_price);
            System.out.println(statement);
            statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        response.sendRedirect(request.getContextPath() + "/goods?method=select_goods");
    }

    private void selectById(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        System.out.println("GoodServlet.selectById()");
        String good_id = request.getParameter("good_id");
        System.out.println(good_id);
        List<goods> list = new ArrayList<goods>();
        try {
            Connection connection = null;
            PreparedStatement statement = null;
            connection = JDBCUtil.getConnection();
            String sql = "select * from goods where good_id=?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1,Integer.parseInt(good_id));
            System.out.println(statement);
            ResultSet resultSet = statement.executeQuery();
            while (resultSet.next()){
                int good_id1 = resultSet.getInt("good_id");
                String good_name = resultSet.getString("good_name");
                String good_spec = resultSet.getString("good_spec");
                double good_price = resultSet.getDouble("good_price");
                goods good = new goods(good_id1 ,good_name, good_spec, good_price);
                request.setAttribute("good", good);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        for (goods good : list) {
            System.out.println(good);
        }

        request.getRequestDispatcher("/goods_update.jsp").forward(request,response);


    }

    private void add_goods(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("UserServlet.add()");
        String good_id = request.getParameter("good_id");
        String good_name = request.getParameter("good_name");
        String good_spec = request.getParameter("good_spec");
        String good_price = request.getParameter("good_price");

        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "insert into goods(good_id,good_name,good_spec,good_price) values(?,?,?,?)";
            statement = connection.prepareStatement(sql);
            statement.setString(1, good_id);
            statement.setString(2, good_name);
            statement.setString(3, good_spec);
            statement.setString(4,good_price);
            System.out.println(statement);
            statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        //删除完之后，重定向到查找所有界面
        response.sendRedirect(request.getContextPath() + "/goods?method=select_goods");
    }

    private void select_goods(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        List<goods> list = new ArrayList<goods>();
        try {
            connection = JDBCUtil.getConnection();
            String sql = "select good_id,good_name,good_spec,good_price from goods";
            statement = connection.prepareStatement(sql);
            System.out.println(statement);
            resultSet = statement.executeQuery();
            while (resultSet.next()) {
                //每遍历一次查出一个User
                int good_id = resultSet.getInt("good_id");
                String good_name =  resultSet.getString("good_name");
                String good_spec = resultSet.getString("good_spec");
                double good_price = resultSet.getDouble("good_price");

                goods good = new goods(good_id,good_name,good_spec,good_price);
                list.add(good);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        for (goods good : list) {
            System.out.println(good);
        }

        //把查找出来的数据放到req域对象里面（一块内存区域）
        request.setAttribute("good_list", list);
        //转发到user_list.jsp页面进行展示
        request.getRequestDispatcher("/goods_list.jsp").forward(request, response);
    }

    private void deleteById(HttpServletRequest request ,HttpServletResponse response)throws IOException{
        System.out.println("GoodServlet.deleteById()");
        String good_id = request.getParameter("good_id");
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "delete from goods where good_id = ?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,Integer.parseInt(good_id));
            System.out.println(preparedStatement);
            preparedStatement.executeUpdate();
        }
        catch (SQLException e){
            e.printStackTrace();
        }

        response.sendRedirect(request.getContextPath()+"/goods?method=select_goods");
    }








}
